$(document).ready(function() {
	var busy_count = 0;
	
	updateAllLinkInfo();

	$('#custom-link').typeWatch({
		captureLength: 0,
		callback: updateAllLinkInfo
	});

	$('#custom-have').typeWatch({
		captureLength: 0,
		callback: function() {
			updateLinkInfo($('#custom-have'));
		}
	});

	$('#custom-dont').typeWatch({
		captureLength: 0,
		callback: function() {
			updateLinkInfo($('#custom-dont'));
		}
	});
	
	$('#custom-match_isbn, #custom-match_missing').click(function() {
		if(matchByIsbn()) {
			$('#field-missing').hide();
		} else {
			$('#field-missing').show();
			$('#custom-missing').focus();
		}
	});
	
	$('#custom-test').click(function() {
		var data = getCustomData();
		
		var site = new CustomLibrary(data);
				
		var $test = $('#custom-form .test-results').text('').removeClass('succeeded').removeClass('failed').show();
		
		$test.show();
		
		var trace = function(msg) {
			$test.html($test.html() + msg + "\n");
		};
		
		trace("Testing configuration for '" + site.title + "'...");
		
		testLibrary();

		var finishedTest = function(error) {
			$test.addClass(error ? 'failed' : 'succeeded');
		};		
		
		var traceResult = function(type, isbn, url, found, expectedBook) {
			var cssClass = found != expectedBook ? 'error' : 'success';
			trace("Search for <b>" + type + "</b> book <a href='" + url + "'>" + isbn + "</a> returned: <span class='" + cssClass + "'>" + (found ? "yes" : "no") + "</span> "
			 + (found ? "(book found)" : "(book not found)"));
		};

		
		function testLibrary() {

			var url = '/library/test';
	
			var data = {
				title: $('#custom-title').val(),
				link: $('#custom-link').val(),
				have: $('#custom-have').val(),
				dont: $('#custom-dont').val()
			};
			
			if(matchByIsbn()) {
				data.match_isbn = true;
			} else {
				data.missing = $('#custom-missing').val()
			}
	

			jQuery.ajax({
	
				url: url,
				
				data: data,
	
				success: function(data, textStatus, jqXHR) {
					var reply = $.parseJSON(jqXHR.responseText);
					var failed = false;
					
					if(reply.error) {
						trace("ERROR: " + reply.error);
						failed = true;
					} else {
						traceResult("existing", reply.library.have, reply.haveUrl, reply.have, true);
						traceResult("missing", reply.library.dont, reply.dontUrl, reply.dont, false);
						if(reply.existing) {
							trace("<span class='error'>A library record with this title already exists!</span>");
							failed = true;
						} else {
							trace("<span class='success'>Name for new library is unique.</span>");
							failed = !reply.have || reply.dont;
						}
					}
					
					finishedTest(failed);
				},
	
				error: function(jqXHR, textStatus, errorThrown) {
					trace("ERROR: " + textStatus);
					finishedTest(true);
				}
			});
		}
		
		return false;
	});

});

/*
	Callbacks used by edit-library.js
*/

	function getCustomData() {
		var data = { name: '__custom__' };
		$('#custom-form input:text').each(function(){
			data[$(this).attr('name')] = $(this).val();
		});

		if(matchByIsbn()) {
			data.match_isbn = true;
			delete(data.missing);
		}
		
		return data;
	};
